package com.tencent.cloud.iov.util.lifecycle;

import android.app.Activity;
import android.os.Bundle;

import com.tencent.mars.xlog.Log;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Activity 生命周期计数器
 *
 * @author showxiao
 * @date 2018/6/26
 */
public class ActivityStackManager extends SimpleActivityLifecycleCallbacks {

    private static ActivityStackManager sInstance = new ActivityStackManager();

    private int mCreateCount;
    private int mStartCount;
    private int mResumeCount;
    private List<Activity> mActivities;
    private List<IActivityStackListener> mActivityStackListeners;
    private Activity mTopActivity;

    public static ActivityStackManager getInstance() {
        return sInstance;
    }

    private ActivityStackManager() {
        mCreateCount = 0;
        mStartCount = 0;
        mResumeCount = 0;
        mActivities = new CopyOnWriteArrayList<>();
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        super.onActivityCreated(activity, savedInstanceState);
        ++mCreateCount;
        mActivities.add(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        super.onActivityStarted(activity);
        ++mStartCount;
        mTopActivity = activity;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        super.onActivityResumed(activity);
        ++mResumeCount;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        super.onActivityPaused(activity);
        --mResumeCount;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        super.onActivityStopped(activity);
        --mStartCount;
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        super.onActivityDestroyed(activity);
        mActivities.remove(activity);
        if (--mCreateCount == 0) {
            if (mActivityStackListeners != null) {
                for (IActivityStackListener listener : mActivityStackListeners) {
                    listener.onActivityStackEmpty();
                }
                mActivityStackListeners.clear();
                mActivityStackListeners = null;
            }
            mTopActivity = null;
        }
    }

    public int getStartCount() {
        return mStartCount;
    }

    public int getResumeCount() {
        return mResumeCount;
    }

    public Activity getTopActivity() {
        return mTopActivity;
    }

    public void addActivityStackListener(IActivityStackListener listener) {
        if (mActivityStackListeners == null) {
            mActivityStackListeners = new CopyOnWriteArrayList<>();
        }

        if (!mActivityStackListeners.contains(listener)) {
            mActivityStackListeners.add(listener);
        }
    }

    public void removeActivityStackListener(IActivityStackListener listener) {
        if (mActivityStackListeners != null && mActivityStackListeners.contains(listener)) {
            mActivityStackListeners.remove(listener);
        }
    }


    public void clearStack() {
        for (Activity activity : mActivities) {
            activity.finish();
        }
        mActivities.clear();
    }

    public void clearNumberStack(int number) {
        for (int i = 0; i < number; i++) {
            mActivities.get(mCreateCount - 1).finish();
            mCreateCount--;
        }
    }
}
